package com.ph.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class SecurityInterceptor implements Interceptor {
	private static final String LOGON_PAGE = "global";

	public void init() {
	}

	@Override
	public void destroy() {
	}

	// 重写方法拦截器拦截方法
	@Override
	public String intercept(ActionInvocation actionExecute) throws Exception {
//		Object action = actionExecute.getAction();
		ActionContext ac = actionExecute.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) ac
				.get(ServletActionContext.HTTP_REQUEST);
		HttpSession session = request.getSession();
		String actionName = actionExecute.getProxy().getActionName();

		if (actionName.equals("index") || actionName.equals("login")
				|| actionName.equals("logout")) {
			actionExecute.invoke();
		} else {
			if (session.getAttribute("userId") != null) {
				actionExecute.invoke();
			} else {
				return LOGON_PAGE;
			}
		}

		return Action.SUCCESS;
	}
}
